package com.achievo.know.application.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.application.LinkApplication;
import com.achievo.know.domain.FriendLink;
import com.achievo.know.domain.FriendLinkRepository;
import com.achievo.know.domain.FriendLink.LinkType;
@Service
public class LinkApplicationImpl implements LinkApplication {
	
	
	
	@Override
	public List<FriendLink> findLinks(LinkType in) {
		return friendLinkRepository.findLinks(in);
	}
	
	@Override
	@Transactional
	public void add(FriendLink link) {
		Integer max=(Integer) friendLinkRepository.getSingleResult("select max(link.order) from FriendLink link where link.type=?", new Object[]{link.getType()});
		if(max==null)
			max=0;
		link.setOrder(max+1);
		friendLinkRepository.save(link);
	}

	@Override
	@Transactional
	public void edit(FriendLink link) {
		FriendLink old=friendLinkRepository.get(link.getId());
		old.setTitle(link.getTitle());
		old.setUrl(link.getUrl());
		friendLinkRepository.save(old);
		
	}

	@Override
	public FriendLink get(long id) {
		return friendLinkRepository.get(id);
	}

	@Override
	@Transactional
	public void batchDelete(String[] idArr) {
		for(String id:idArr){
			FriendLink link=friendLinkRepository.get(Long.parseLong(id));
			friendLinkRepository.remove(link);
		}
	}

	@Override
	@Transactional
	public void top(long id) {
		FriendLink link=friendLinkRepository.get(id);
		Integer max=(Integer) friendLinkRepository.getSingleResult("select max(link.order) from FriendLink link where link.type=?", new Object[]{link.getType()});
		if(max==null)
			max=0;
		link.setOrder(max+1);
		friendLinkRepository.save(link);
	}
	


	@Override
	@Transactional
	public void up(long id) {
		FriendLink link=friendLinkRepository.get(id);
		FriendLink up=(FriendLink) friendLinkRepository.getUp(link);
		if(up==null)
			return;
		int temp=link.getOrder();
		link.setOrder(up.getOrder());
		up.setOrder(temp);
		friendLinkRepository.save(link);
		friendLinkRepository.save(up);
	}



	@Override
	@Transactional
	public void down(long id) {
		FriendLink link=friendLinkRepository.get(id);
		FriendLink down=(FriendLink) friendLinkRepository.getDown(link);//.getSingleResult("from FriendLink link where link.type=? and link.order>?", new Object[]{link.getType(),link.getOrder()});
		if(down==null)
			return;
		int temp=link.getOrder();
		link.setOrder(down.getOrder());
		down.setOrder(temp);
		friendLinkRepository.save(link);
		friendLinkRepository.save(down);
	}



	@Autowired
	private FriendLinkRepository friendLinkRepository;

	public FriendLinkRepository getFriendLinkRepository() {
		return friendLinkRepository;
	}

	public void setFriendLinkRepository(FriendLinkRepository friendLinkRepository) {
		this.friendLinkRepository = friendLinkRepository;
	}
	
}
